﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Models;
//using S7.Net;
using HPSocket;
using HPSocket.Tcp;
using SerialPortFunction;
using HpSocketFunction;
using HslCommunication;
using HslCommunication.Profinet.Siemens;

namespace BLL
{
    class AllTypeConnectionHelper
    {
        List<DeviceConnMessage> _DeviceConnList;
        public AllTypeConnectionHelper(List<DeviceConnMessage> InDeviceConnList)
        {
            this._DeviceConnList = InDeviceConnList;
        }
        #region TcpServer
        public void IniTcpServer(ref HpSocketServer SocketDevice, string DeviceName)
        {
            DeviceConnMessage ServerConnMessage = _DeviceConnList.Where(x => x.DeviceName == DeviceName).FirstOrDefault();
            if (SocketDevice == null)
            {
                SocketDevice = new HpSocketServer(ServerConnMessage.ServerAdress, ServerConnMessage.ServerPort);
            }
            else
            {
                if (!SocketDevice.IsHasStarted)
                {
                    SocketDevice.Close();
                    SocketDevice.Start();
                }
            }
        }
        #endregion
        #region TcpClient
        public void IniTcpClient(ref HpSocketClient SocketDevice, string DeviceName)
        {
            DeviceConnMessage ClientConnMessage = _DeviceConnList.Where(x => x.DeviceName == DeviceName).FirstOrDefault();
            if (SocketDevice == null)
            {
                SocketDevice = new HpSocketClient(ClientConnMessage.ServerAdress, ClientConnMessage.ServerPort, ClientConnMessage.BindAdress, ClientConnMessage.BindPort);
            }
            else
            {
                if (!SocketDevice.IsConnection)
                {
                    SocketDevice.Close();
                    SocketDevice.Start();
                }
            }
        }
        #endregion
        #region Com
        public void IniCom(ref SerialHelper ComDevice, string DeviceName)
        {
            DeviceConnMessage ComConnMessage = _DeviceConnList.Where(x => x.DeviceName == DeviceName).FirstOrDefault();
            if (ComDevice == null)
            {
                ComDevice = new SerialHelper();
            }
            else
            {
                if (!ComDevice.IsOpen)
                {
                    ComDevice.closePort();
                    ComDevice.Open(ComConnMessage.PortName, ComConnMessage.BaudRate,
                    ComConnMessage.Parity, ComConnMessage.DataBits, ComConnMessage.StopBits);
                }
            }
        }
        #endregion
        #region SiemensS7
        public void IniSiemensS7_1200(ref SiemensS7Net PlcDevice, ref OperateResult PlcConnect, string DeviceName)
        {
            DeviceConnMessage PlcConnMessage = _DeviceConnList.Where(x => x.DeviceName == DeviceName).FirstOrDefault();
            if (PlcDevice == null)
            {

                PlcDevice = new SiemensS7Net(SiemensPLCS.S1200, PlcConnMessage.ServerAdress);
            }
            if (PlcConnect == null)
            {
                PlcConnect = PlcDevice.ConnectServer();
            }
            if (!PlcConnect.IsSuccess&& !(PlcConnect == null))
            {
                PlcDevice.ConnectClose();
                PlcDevice = new SiemensS7Net(SiemensPLCS.S1200, PlcConnMessage.ServerAdress);
            }

        }
        #endregion
    }
}
